package com.atguigu.product.products.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.product.products.vo.reqVo.AttrSaveReqVo;
import com.atguigu.product.products.vo.respVo.AttrQueryRespVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.product.products.entity.PmsAttrEntity;
import com.atguigu.product.products.service.PmsAttrService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 商品属性
 *
 * @author qhw
 * @email qhw@gmail.com
 * @date 2021-08-03 19:57:10
 */
@RestController
@RequestMapping("product/attr")
public class PmsAttrController {
    @Autowired
    private PmsAttrService pmsAttrService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("products:pmsattr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = pmsAttrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    @RequiresPermissions("products:pmsattr:info")
    public R info(@PathVariable("attrId") Long attrId) {
        PmsAttrEntity pmsAttr = pmsAttrService.getById(attrId);

        return R.ok().put("attr", pmsAttr);
    }

    /**
     * 信息
     */
    @RequestMapping("{attr}/list/{categoryId}")
    //@RequiresPermissions("products:pmsattr:info")
    public R baseList(@PathVariable("categoryId") Long categoryId, @RequestParam Map<String, Object> params,
                      @PathVariable("attr") String attr) {
        PageUtils attrQueryRespVos = pmsAttrService.baseList(categoryId, params,attr);
        return R.ok().put("page", attrQueryRespVos);
    }

    /**
     * 保存--并且保存数据到分组属性表中
     */
    @RequestMapping("/save")
    //@RequiresPermissions("products:pmsattr:save")
    public R save(@RequestBody AttrSaveReqVo attrSaveReqVo) {
        pmsAttrService.saveMain(attrSaveReqVo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("products:pmsattr:update")
    public R update(@RequestBody PmsAttrEntity pmsAttr) {
        pmsAttrService.updateById(pmsAttr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("products:pmsattr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        pmsAttrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
